package com.magicode.portal.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.BaseQuarterSet;
import com.magicode.cms.entiy.base.BroadPlatform;
import com.magicode.cms.entiy.base.Dictionary;
import com.magicode.cms.entiy.base.Enterprise;
import com.magicode.cms.entiy.base.NewsPersonRela;
import com.magicode.cms.entiy.base.NewsProgramRela;
import com.magicode.cms.entiy.base.Program;
import com.magicode.cms.entiy.base.ProgramPersonRela;
import com.magicode.cms.entiy.base.TvFilmPerson;
import com.magicode.cms.entiy.base.WsbeClick;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.portal.service.ISecProgramService;

@Service("secProgramService")
@Transactional
public class SecProgramServiceImpl extends MainServiceImpl implements ISecProgramService{

	@Override
	public Program get(String id) {
		String hql = " from "+Program.class.getName()+" p "
				+ " left join fetch p.mainPic "
				+ " left join fetch p.area"
				+ " left join fetch p.programType"
//				+ " left join fetch p.enterprise"
//				+ " left join fetch p.director"
//				+ " left join fetch p.platform"
				+ " left outer join fetch p.relaPics "
				+ " where p.id=? ";
		Program program = null;
		try {
			program = (Program) this.commonDaoService.getByHQL(hql, new String[]{id});
			//首播平台
			List<BroadPlatform> bpfList = new ArrayList();
			bpfList = this.platformList(id);
			program.setPlatformList(bpfList);
			//导演与相关影视人
			program = this.programPerson(program);
			//制作机构
			List<Enterprise> entList = new ArrayList();
			entList = this.enterpriseList(id);
			program.setEnterpriseList(entList);
			//网络播出平台
			List<Dictionary> dicList = new ArrayList();
			dicList = this.netPlatformList(id);
			program.setNetPlatformList(dicList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return program;
	}

	/**
	 * 关联多个首播平台
	 * @return
	 */
	public List<BroadPlatform> platformList(String id){
		List<BroadPlatform> broadPlatformList = new ArrayList();
		String sql = " select p from "+BroadPlatform.class.getName()+" p , "+Program.class.getName()+" t "
				+ " where p in elements(t.platformList) and t.id = :id order by p.id desc ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			broadPlatformList = this.commonDaoService.queryList(sql, map);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return broadPlatformList;
	}
	
	/**
	 * 获取   导演与相关影视人
	 * @return
	 */
	public Program programPerson(Program program){
		List<ProgramPersonRela> properList = new ArrayList();
		List<ProgramPersonRela> personList = new ArrayList();
		List<ProgramPersonRela> directorList = new ArrayList();
		String sql = " select ppr from "+ProgramPersonRela.class.getName()+" ppr left outer join fetch ppr.program p left outer join fetch ppr.person t "
				+ " where p.id = :id order by ppr.orderCode asc ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", program.getId());
		try{
			properList = this.commonDaoService.queryList(sql, map);
		} catch (Exception e1){
			e1.printStackTrace();
		}
		if(null!=properList && properList.size()>0){
			for (ProgramPersonRela programPersonRela : properList) {
				if(null != programPersonRela.getType()){
					if(programPersonRela.getType()==1){
						//相关影视人
						personList.add(programPersonRela);
					}
					if(programPersonRela.getType()==2){
						//导演
						directorList.add(programPersonRela);
					}
				}
			}
		}
		program.setPersonList(personList);
		program.setDirectorList(directorList);
		return program;
	}
	
	/**
	 * 关联多个制作公司
	 * @param id
	 * @return
	 */
	public List<Enterprise> enterpriseList(String id){
		List<Enterprise> entList = new ArrayList();
		String sql = " select p from "+Enterprise.class.getName()+" p , "+Program.class.getName()+" t "
				+ " where p in elements(t.enterpriseList) and t.id = :id order by p.orderCode asc , p.clickCount desc ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			entList = this.commonDaoService.queryList(sql, map);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return entList;
	}
	
	/**
	 * 关联多个网络播出平台
	 * @param id
	 * @return
	 */
	public List<Dictionary> netPlatformList(String id){
		List<Dictionary> dicList = new ArrayList();
		String sql = " select p from "+Dictionary.class.getName()+" p , "+Program.class.getName()+" t "
				+ " where p in elements(t.netPlatformList) and t.id = :id order by p.orderCode asc ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			dicList = this.commonDaoService.queryList(sql, map);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return dicList;
	}
	
	@Override
	public List<TvFilmPerson> getpersonList(String id) {
		List<TvFilmPerson> personList = new ArrayList();
		String hql = " select t from "+ProgramPersonRela.class.getName()+" ppr left join ppr.program p "
				+ " , "+TvFilmPerson.class.getName()+" t "
				+ " where ppr.person = t and p.id = :id and ppr.type = 1 "
				+ " order by ppr.orderCode asc , t.clickCount desc ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try{
			personList = this.commonDaoService.queryList(hql, map);
		} catch (Exception e1){
			e1.printStackTrace();
		}
		return personList;
	}
	
	@Override
	public List<Article> getArticleList(String id) {
		List<Article> artList = new ArrayList();
		String hql = " select n from "+NewsProgramRela.class.getName()+" np  left join np.program p , "+Article.class.getName()+" n " 
				+ " where p.id = :id and np.news = n and n.status = 2 and n.valid = 1 order by n.subcribeTime desc , np.orderCode asc  ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			artList = this.commonDaoService.queryList(hql, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return artList;
	}
	
	/**
	 * 获取 "季/集、网络播出平台、节目冠名、节目收视情况"的数据
	 * @param program
	 * @return
	 */
//	public Program baseQuarterSet(Program program){
//		String id = program.getId();
//		List<BaseQuarterSet> bqsList = new ArrayList();
//		//  季/集(1)
//		List<BaseQuarterSet> qslList = new ArrayList();
//		//网络播出平台(4)
//		List<BaseQuarterSet> nbpList = new ArrayList();
//		//节目冠名(2)
//		List<BaseQuarterSet> namList = new ArrayList();
//		//节目收视情况(3)
//		List<BaseQuarterSet> arlList = new ArrayList();
//		if(null!=id && !"".equals(id)){
//			String hql = " select b from "+BaseQuarterSet.class.getName()
//					+" b left join b.program p where p.id = :id ";
//			Map<String, String> map = new HashMap<String, String>();
//			map.put("id", id);
//			try {
//				bqsList = this.commonDaoService.queryList(hql, map);
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//			if(null!=bqsList && bqsList.size()>0){
//				for (BaseQuarterSet baseQuarterSet : bqsList) {
//					if(baseQuarterSet.getType()==1){
//						qslList.add(baseQuarterSet);
//					}
//					if(baseQuarterSet.getType()==2){
//						namList.add(baseQuarterSet);
//					}
//					if(baseQuarterSet.getType()==3){
//						arlList.add(baseQuarterSet);
//					}
//					if(baseQuarterSet.getType()==4){
//						nbpList.add(baseQuarterSet);
//					}
//				}
//				program.setQuarterSetList(qslList);
//				program.setNetBroadcastPlatform(nbpList);
//				program.setNamingList(namList);
//				program.setAudienceRatingList(arlList);
//			}
//		}
//		return program;
//	}
	
	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;

	@Override
	public List<TvFilmPerson> getpersonList(String id, Integer type) {
		// TODO Auto-generated method stub
		return null;
	}

}
